﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BL.Entity;
using BL.Service;

namespace LTO
{
    /// <summary>
    /// Interaction logic for PaySetUp.xaml
    /// </summary>
    public partial class PaySetUp2 : Window
    {
        tbl_PayService PayService = new tbl_PayService();
        public string[] NumberTypeCollection = { "O3", "T3", "O2", "T2", "O1", "U3", "U2", "U1" };
        public PaySetUp2()
        {
            InitializeComponent();
            HotKey _HotKey_Esc = new HotKey(Key.Escape, KeyModifier.None, OnCloseWindows);

        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            List<Pay> paylist = PayService.GetPay();
            if (paylist.Count() > 0)
            {
                foreach (Pay item in paylist)
                {
                    TextBox txBuy = this.FindName(string.Format("txt{0}Buy", item.NumType)) as TextBox;
                    TextBox txPay = this.FindName(string.Format("txt{0}Pay", item.NumType)) as TextBox;
                    TextBox txDis = this.FindName(string.Format("txt{0}Dis", item.NumType)) as TextBox;
                    TextBox txMinSt = this.FindName(string.Format("txt{0}MinSt", item.NumType)) as TextBox;
                    TextBox txMinEn = this.FindName(string.Format("txt{0}MinEn", item.NumType)) as TextBox;
                    TextBox txLimit = this.FindName(string.Format("txt{0}Limit", item.NumType)) as TextBox;
                    txBuy.Text = String.Format("{0:#,#}", item.Buy);
                    txPay.Text = String.Format("{0:#,#}",item.PayPrice);
                    txDis.Text = String.Format("{0:#,#}",item.Discount);
                    txMinSt.Text = String.Format("{0:#,#}",item.MinStart);
                    txMinEn.Text = String.Format("{0:#,#}",item.MinEnd);
                    txLimit.Text = String.Format("{0:#,#}", item.LimitBuy);

                }
            }
        }

        private void OnCloseWindows(HotKey hotKey)
        {
            this.Close();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                List<Pay> paylist = new List<Pay>();
                foreach (string item in NumberTypeCollection)
                {
                    Pay payItem = new Pay();
                    TextBox txBuy = this.FindName(string.Format("txt{0}Buy", item)) as TextBox;
                    TextBox txPay = this.FindName(string.Format("txt{0}Pay", item)) as TextBox;
                    TextBox txDis = this.FindName(string.Format("txt{0}Dis", item)) as TextBox;
                    TextBox txMinSt = this.FindName(string.Format("txt{0}MinSt", item)) as TextBox;
                    TextBox txMinEn = this.FindName(string.Format("txt{0}MinEn", item)) as TextBox;
                    TextBox txLimit = this.FindName(string.Format("txt{0}Limit", item)) as TextBox;

                    payItem.Buy = Gobalsource.utility.ConvertToDecimal(txBuy.Text);
                    payItem.PayPrice = Gobalsource.utility.ConvertToDecimal(txPay.Text);
                    payItem.Discount = Gobalsource.utility.ConvertToDecimal(txDis.Text);
                    payItem.MinStart = Gobalsource.utility.ConvertToDecimal(txMinSt.Text);
                    payItem.MinEnd = Gobalsource.utility.ConvertToDecimal(txMinEn.Text);
                    payItem.LimitBuy = Gobalsource.utility.ConvertToDecimal(txLimit.Text);
                    payItem.NumType = item;
                    payItem.UserID = Gobalsource.UserIDLogin;
                    payItem.UpdateBy = Gobalsource.UserNameLogin;
                    payItem.CreateBy = Gobalsource.UserNameLogin;
                    paylist.Add(payItem);
                }
                PayService.SavePay(paylist);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
           
        }

        private void txtO3Min_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtMin = sender as TextBox;
            string getPrefixName = txtMin.Name.Substring(3, 2);
            TextBox txMinSt = this.FindName(string.Format("txt{0}MinSt", getPrefixName)) as TextBox;
            txMinSt.Text = txtMin.Text;
        }

        private void txtO3Limit_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txtMax = sender as TextBox;
            string getPrefixName = txtMax.Name.Substring(3, 2);
            TextBox txMinEn = this.FindName(string.Format("txt{0}MinEn", getPrefixName)) as TextBox;
            txMinEn.Text = txtMax.Text;
        }

    }
}
